/**
 * @author Jiabin
 * 
 */
public class UdpChat
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        if (args.length != 2 && args.length != 5)
        {
            System.out.println(StringTable.ERR_INCORRECT_NUM_OF_ARGS);
            System.exit(1);
        }
        else
        {
            if (args[0].equals("-s"))
            {
                if (!checkServerArgs(args))
                {
                    System.out.println(StringTable.ERR_INCORRECT_SERVER_MODE_ARGS);
                    System.exit(1);
                }

                Server server = new Server(Integer.parseInt(args[1]));
                server.run();
            }
            else if (args[0].equals("-c"))
            {
                if (!checkClientArgs(args))
                {
                    System.out.println(StringTable.ERR_INCORRECT_CLIENT_MODE_ARGS);
                    System.exit(1);
                }

                Client client = new Client(args[1], args[2], Integer.parseInt(args[3]), Integer.parseInt(args[4]));
                client.run();
            }
            else
            {
                System.out.println(StringTable.ERR_INCORRECT_SERVER_MODE_ARGS);
                System.exit(1);
            }
        }
    }

    /**
     * @param args
     * @return
     */
    private static boolean checkServerArgs(String[] args)
    {
        if (args.length != 2)
            return false;
        return checkPortNumber(args[1]);
    }

    /**
     * @param arg
     * @return
     */
    private static boolean checkPortNumber(String arg)
    {
        int port = -1;
        try
        {
            port = Integer.parseInt(arg);
        }
        catch (NumberFormatException e)
        {
            return false;
        }

        if (port < 1024 || port > 65535)
            return false;
        else
            return true;
    }

    /**
     * @param args
     * @return
     */
    private static boolean checkClientArgs(String[] args)
    {
        if (args.length != 5)
            return false;

        if (!checkIP(args[2]))
            return false;

        if (!checkPortNumber(args[3]) || !checkPortNumber(args[4]))
            return false;

        return true;
    }

    /**
     * @param addr
     * @return
     */
    private static boolean checkIP(String addr)
    {
        String[] segaments = addr.split("\\.");
        if (segaments.length != 4)
            return false;
        else
        {
            for (String segament : segaments)
            {
                int value;
                try
                {
                    value = Integer.parseInt(segament);
                }
                catch (NumberFormatException e)
                {
                    return false;
                }
                if (value > 255 || value < 0)
                    return false;
            }
        }
        return true;
    }
}
